/*
 * @(#)HexButton.java	1.0 Apr 27, 2008
 *
 *	The MIT License
 *
 *	Copyright (c) 2008 Malachi de AElfweald <malachid@gmail.com>
 *
 *	Permission is hereby granted, free of charge, to any person obtaining a copy
 *	of this software and associated documentation files (the "Software"), to deal
 *	in the Software without restriction, including without limitation the rights
 *	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *	copies of the Software, and to permit persons to whom the Software is
 *	furnished to do so, subject to the following conditions:
 *
 *	The above copyright notice and this permission notice shall be included in
 *	all copies or substantial portions of the Software.
 *
 *	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *	THE SOFTWARE.
 */
package org.eoti.swing.hex;

import org.eoti.awt.geom.Hexagon;
import org.eoti.awt.ColorUtil;
import org.eoti.awt.WebColor;

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.beans.ConstructorProperties;

public class HexButton
extends JButton
		implements ComponentListener, KeyListener
{
	protected Hexagon hex = null;
	protected int maxStringWidth = 0;

	public HexButton(){
		super();
		setup();
	}

	public HexButton(Icon icon){
		super(icon);
		setup();
	}

	@ConstructorProperties({"text"})
	public HexButton(String text){
		super(text);
		setup();
	}

	public HexButton(Action a){
		super(a);
		setup();
	}

	public HexButton(String text, Icon icon){
		super(text, icon);
		setup();
	}

	public HexButton(Hexagon hex){
		super();
		setHex(hex);
		setup();
	}

	public HexButton(Hexagon hex, Icon icon){
		super(icon);
		setHex(hex);
		setup();
	}

	@ConstructorProperties({"text"})
	public HexButton(Hexagon hex, String text){
		super(text);
		setHex(hex);
		setup();
	}

	public HexButton(Hexagon hex, Action a){
		super(a);
		setHex(hex);
		setup();
	}

	public HexButton(Hexagon hex, String text, Icon icon){
		super(text, icon);
		setHex(hex);
		setup();
	}

	public Hexagon getHex(){return hex;}
	public void setHex(Hexagon hex){this.hex = hex;}

	protected void setup()
	{
		setOpaque(false);
		//this.setBorderPainted(false);
		this.setContentAreaFilled(false);
		this.setMargin(new Insets(0,0,0,0));
//		this.setFocusPainted(false); // ???
		this.addComponentListener(this);
		this.addKeyListener(this);
//		Font buttonFont  = (Font)UIManager.get("Button.font");
//		Frame rootFrame = JOptionPane.getRootFrame();
//		FontMetrics metrics = rootFrame.getFontMetrics(buttonFont);
//		maxStringWidth = metrics.stringWidth(getText());

	}

	protected void ensureHexIsCurrent()
	{
		if((hex != null) && (hex.getBounds().equals(getBounds())))
			return; // everything is current;

// TODO This is not lining up properly!!! It is off the screen!!!
		int centerX = getX() + (getWidth() /2);
		int centerY = getY() + (getHeight()/2);
		int size = Math.max(getHeight(), getWidth());
		hex = new Hexagon(centerX, centerY, size);
		this.setPreferredSize(new Dimension(size, size));
		this.setBounds(hex.getBounds());
System.out.format("BOUNDS: " + getBounds());
	}

	public void paintComponent(Graphics g)
	{
		ensureHexIsCurrent();
		Graphics2D g2 = (Graphics2D)g;
		RenderingHints hints = new RenderingHints(
				RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON
		);
		g2.setRenderingHints(hints);

		Color color = null;
		if(isFocusPainted() && hasFocus())
			color = UIManager.getColor("Button.highlight");

		if(color == null)
			color = getBackground();

		if(color == null)
			color = UIManager.getColor("Button.background");

		if(color == null)
			color = WebColor.CornSilk.getColor();

		if(getModel().isArmed() && getModel().isPressed())
			g2.setColor(color.brighter().brighter());
		else
			g2.setColor(color);

		g2.fillPolygon(hex);
		super.paintComponent(g);
	}

	public void paintBorder(Graphics g)
	{
		ensureHexIsCurrent();
		/**
		 * Paint component is called before paint border
		 * so can't just rely on center being overdrawn
		 */
		Graphics2D g2 = (Graphics2D)g;
		RenderingHints hints = new RenderingHints(
				RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON
		);
		g2.setRenderingHints(hints);

		Color shadow = UIManager.getColor("Button.darkShadow");
		if(shadow == null)
			shadow = WebColor.BurlyWood.getColor();

		if(getModel().isArmed() && getModel().isPressed())
			g2.setColor(shadow.darker().darker());
		else
			g2.setColor(shadow);

		g2.drawPolygon(hex);
	}

	public boolean contains(int x, int y)
	{
		ensureHexIsCurrent();
		Point p = new Point(x,y);
		SwingUtilities.convertPointFromScreen(p, this);
		return hex.contains(x, y);
	}

	public void componentResized(ComponentEvent e){
		ensureHexIsCurrent();
	}

	public void componentMoved(ComponentEvent e){
	}

	public void componentShown(ComponentEvent e){
	}

	public void componentHidden(ComponentEvent e){
	}

	public void keyTyped(KeyEvent e){
	}

	public void keyPressed(KeyEvent e)
	{
		if((e.getSource() == this) && (e.getKeyCode() == KeyEvent.VK_ENTER))
		{
		  this.doClick();
		}
	}

	public void keyReleased(KeyEvent e){
	}
}
